package com.sunda.spmsorder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.entity.OrderKeyValueConfig;
import com.sunda.spmsorder.mapper.OrderKeyValueConfigMapper;
import com.sunda.spmsorder.service.IOrderKeyValueConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 订单请购码值配置信息表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2020-12-22
 */
@Service
@CacheConfig(cacheNames = "kvConfig")
public class OrderKeyValueConfigServiceImpl extends ServiceImpl<OrderKeyValueConfigMapper, OrderKeyValueConfig> implements IOrderKeyValueConfigService {

    private static final String CACHE_KVCONFIG = "KV";

    @Autowired
    OrderKeyValueConfigMapper orderKeyValueConfigMapper;

    @Override
//    @Cacheable
    public List<OrderKeyValueConfig> getAllKVConfig() {
        return orderKeyValueConfigMapper.selectList(new QueryWrapper<OrderKeyValueConfig>().orderByAsc("LOWER(FIELD_KEY)").orderByAsc("KEY_CODE"));
    }

    @Override
    public List<OrderKeyValueConfig> getByFieldKey(String fieldKey) {
        return orderKeyValueConfigMapper.selectList(new QueryWrapper<OrderKeyValueConfig>().eq("FIELD_KEY", fieldKey).orderByAsc("KEY_CODE"));
    }

    /***
     * {
     *         "uuid": "06a6373233dd4f3b861af961f747524f",
     *         "fieldKey": "orderWerksSPMSStatus",
     *         "fieldName": "工厂请购单SPMS状态",
     *         "keyCode": "4",
     *         "keyCodeDesc": "OA审批不通过",
     *         "keyCodeEnDesc": "OA approval failed",
     *         "enabled": "1",
     *         "sortNo": 1000,
     *         "remark": ""
     *       }
     * @param doc
     * @return
     */
    @Override
    public ResponseResult createKVConfig(JSONObject doc) {
        try {
            OrderKeyValueConfig keyValueConfig = new OrderKeyValueConfig();
            keyValueConfig.setFieldKey(doc.getString("fieldKey"));
            keyValueConfig.setFieldName(doc.getString("fieldName"));
            keyValueConfig.setKeyCode(doc.getString("keyCode"));
            keyValueConfig.setKeyCodeDesc(doc.getString("keyCodeDesc"));
            keyValueConfig.setKeyCodeEnDesc(doc.getString("keyCodeEnDesc"));
            keyValueConfig.setEnabled(doc.getString("enabled"));
            keyValueConfig.setSortNo(Integer.parseInt(doc.getString("sortNo")));
            keyValueConfig.setRemark(doc.getString("remark"));
            if (doc.containsKey("uuid") && doc.getString("uuid").length() > 0){
                /** 更新数据 */
                if (orderKeyValueConfigMapper.update(keyValueConfig, new QueryWrapper<OrderKeyValueConfig>()
                        .eq("UUID", doc.getString("uuid"))) > 0){
                    return ResponseResult.success().add("kvList",getAllKVConfig());
                }
                return ResponseResult.error("更新失败");
            }
            /** 新建数据 */
            String uuid = UUID.randomUUID().toString().replaceAll("-","").toLowerCase();
            keyValueConfig.setUuid(uuid);
            if (orderKeyValueConfigMapper.insert(keyValueConfig) > 0){
                return ResponseResult.success().add("kvList",getAllKVConfig());
            }
            return ResponseResult.error("新增失败");
        }catch (Exception e){
            System.out.println(e);
            return ResponseResult.error();
        }
    }

    @Override
    public ResponseResult deleteKCConfig(String uuid) {
        orderKeyValueConfigMapper.delete(new QueryWrapper<OrderKeyValueConfig>().eq("UUID", uuid));
        return ResponseResult.success();
    }
}
